
/*
 * Copyright 2002-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2023原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.beans.factory.config;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Properties;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

/**
 * A {@link FactoryBean} implementation that takes an interface which must have one or more
 * methods with the signatures {@code MyType xxx()} or {@code MyType xxx(MyIdType id)}
 * (typically, {@code MyService getService()} or {@code MyService getService(String id)})
 * and creates a dynamic proxy which implements that interface, delegating to an
 * underlying {@link org.springframework.beans.factory.BeanFactory}.
 *
 * <p>Such service locators permit the decoupling of calling code from
 * the {@link org.springframework.beans.factory.BeanFactory} API, by using an
 * appropriate custom locator interface. They will typically be used for
 * <b>prototype beans</b>, i.e. for factory methods that are supposed to
 * return a new instance for each call. The client receives a reference to the
 * service locator via setter or constructor injection, to be able to invoke
 * the locator's factory methods on demand. <b>For singleton beans, direct
 * setter or constructor injection of the target bean is preferable.</b>
 *
 * <p>On invocation of the no-arg factory method, or the single-arg factory
 * method with a String id of {@code null} or empty String, if exactly
 * <b>one</b> bean in the factory matches the return type of the factory
 * method, that bean is returned, otherwise a
 * {@link org.springframework.beans.factory.NoSuchBeanDefinitionException}
 * is thrown.
 *
 * <p>On invocation of the single-arg factory method with a non-null (and
 * non-empty) argument, the proxy returns the result of a
 * {@link org.springframework.beans.factory.BeanFactory#getBean(String)} call,
 * using a stringified version of the passed-in id as bean name.
 *
 * <p>A factory method argument will usually be a String, but can also be an
 * int or a custom enumeration type, for example, stringified via
 * {@code toString}. The resulting String can be used as bean name as-is,
 * provided that corresponding beans are defined in the bean factory.
 * Alternatively, {@linkplain #setServiceMappings(java.util.Properties) a custom
 * mapping} between service IDs and bean names can be defined.
 *
 * <p>By way of an example, consider the following service locator interface.
 * Note that this interface is not dependent on any Spring APIs.
 *
 * <pre class="code">package a.b.c;
 *
 *public interface ServiceFactory {
 *
 *    public MyService getService();
 *}</pre>
 *
 * <p>A sample config in an XML-based
 * {@link org.springframework.beans.factory.BeanFactory} might look as follows:
 *
 * <pre class="code">&lt;beans&gt;
 *
 *   &lt;!-- Prototype bean since we have state --&gt;
 *   &lt;bean id="myService" class="a.b.c.MyService" singleton="false"/&gt;
 *
 *   &lt;!-- will lookup the above 'myService' bean by *TYPE* --&gt;
 *   &lt;bean id="myServiceFactory"
 *            class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"&gt;
 *     &lt;property name="serviceLocatorInterface" value="a.b.c.ServiceFactory"/&gt;
 *   &lt;/bean&gt;
 *
 *   &lt;bean id="clientBean" class="a.b.c.MyClientBean"&gt;
 *     &lt;property name="myServiceFactory" ref="myServiceFactory"/&gt;
 *   &lt;/bean&gt;
 *
 *&lt;/beans&gt;</pre>
 *
 * <p>The attendant {@code MyClientBean} class implementation might then
 * look something like this:
 *
 * <pre class="code">package a.b.c;
 *
 *public class MyClientBean {
 *
 *    private ServiceFactory myServiceFactory;
 *
 *    // actual implementation provided by the Spring container
 *    public void setServiceFactory(ServiceFactory myServiceFactory) {
 *        this.myServiceFactory = myServiceFactory;
 *    }
 *
 *    public void someBusinessMethod() {
 *        // get a 'fresh', brand new MyService instance
 *        MyService service = this.myServiceFactory.getService();
 *        // use the service object to effect the business logic...
 *    }
 *}</pre>
 *
 * <p>By way of an example that looks up a bean <b>by name</b>, consider
 * the following service locator interface. Again, note that this
 * interface is not dependent on any Spring APIs.
 *
 * <pre class="code">package a.b.c;
 *
 *public interface ServiceFactory {
 *
 *    public MyService getService (String serviceName);
 *}</pre>
 *
 * <p>A sample config in an XML-based
 * {@link org.springframework.beans.factory.BeanFactory} might look as follows:
 *
 * <pre class="code">&lt;beans&gt;
 *
 *   &lt;!-- Prototype beans since we have state (both extend MyService) --&gt;
 *   &lt;bean id="specialService" class="a.b.c.SpecialService" singleton="false"/&gt;
 *   &lt;bean id="anotherService" class="a.b.c.AnotherService" singleton="false"/&gt;
 *
 *   &lt;bean id="myServiceFactory"
 *            class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"&gt;
 *     &lt;property name="serviceLocatorInterface" value="a.b.c.ServiceFactory"/&gt;
 *   &lt;/bean&gt;
 *
 *   &lt;bean id="clientBean" class="a.b.c.MyClientBean"&gt;
 *     &lt;property name="myServiceFactory" ref="myServiceFactory"/&gt;
 *   &lt;/bean&gt;
 *
 *&lt;/beans&gt;</pre>
 *
 * <p>The attendant {@code MyClientBean} class implementation might then
 * look something like this:
 *
 * <pre class="code">package a.b.c;
 *
 *public class MyClientBean {
 *
 *    private ServiceFactory myServiceFactory;
 *
 *    // actual implementation provided by the Spring container
 *    public void setServiceFactory(ServiceFactory myServiceFactory) {
 *        this.myServiceFactory = myServiceFactory;
 *    }
 *
 *    public void someBusinessMethod() {
 *        // get a 'fresh', brand new MyService instance
 *        MyService service = this.myServiceFactory.getService("specialService");
 *        // use the service object to effect the business logic...
 *    }
 *
 *    public void anotherBusinessMethod() {
 *        // get a 'fresh', brand new MyService instance
 *        MyService service = this.myServiceFactory.getService("anotherService");
 *        // use the service object to effect the business logic...
 *    }
 *}</pre>
 *
 * <p>See {@link ObjectFactoryCreatingFactoryBean} for an alternate approach.
 *
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @since 1.1.4
 * @see #setServiceLocatorInterface
 * @see #setServiceMappings
 * @see ObjectFactoryCreatingFactoryBean
 */
/**
 *一个｛@link FactoryBean｝实现，它采用必须具有一个或多个接口的接口
 *具有签名｛@code MyType xxx（）｝或｛@ccode MyType xxx（MyIdType id）｝的方法
 *（通常，｛@code MyService getService（）｝或｛@codeMyService getServices（Stringid）｝）
 *并创建一个实现该接口的动态代理
 *底层{@link.org.springframework.beans.factory.BeanFactory}。
 *
 *＜p＞此类服务定位器允许调用代码与
 *通过使用
 *适当的自定义定位器界面。它们通常用于
 *<b>原型bean</b>，即用于工厂方法
 *为每个调用返回一个新实例。客户端收到对
 *通过setter或构造函数注入的服务定位器，以便能够调用
 *定位器的工厂方法<b> 对于单例bean，直接
 *目标bean的setter或构造函数注入是优选的</b>
 *
 *＜p＞在调用无参数工厂方法或单参数工厂时
 *方法，如果字符串id为｛@code null｝或空字符串
 *<b>工厂中的一个</b>bean与工厂的返回类型匹配
 *方法，则返回该bean，否则返回
 *｛@link org.springframework.beans.factory.NoSuchBeanDefinitionException｝
 *被抛出。
 *
 *＜p＞使用非null（和
 *非空）参数，代理返回
 *{@link.org.springframework.beans.factory.BeanFactory#getBean（字符串）}调用，
 *使用传入id的字符串化版本作为bean名称。
 *
 *＜p＞工厂方法参数通常是字符串，但也可以是
 *int或自定义枚举类型，例如，stringified via
 *｛@code toString｝。生成的String可以原样用作bean名称，
 *前提是在bean工厂中定义了相应的bean。
 *或者，{@linkplain#setServiceMappings（java.util.财产）将自定义
 *可以定义服务ID和bean名称之间的映射}。
 *
 *＜p＞作为示例，考虑以下服务定位器接口。
 *注意，此接口不依赖于任何SpringAPI。
 *
 *<pre-class=“code”>包a.b.c；
 *
 *公共接口ServiceFactory{
 *
 *公共MyService getService（）；
 *}</pre>
 *
 *＜p＞基于XML的示例配置
 *｛@link org.springframework.beans.factory.BeanFactory｝可能如下所示：
 *
 *<pre-class=“code”>&lt；豆类&gt；
 *
 *&lt；！--原型bean，因为我们有状态--&gt；
 *&lt；bean id=“myService”class=“a.b.c.myService”singleton=“false”/&gt；
 *
 *&lt；！--将按*TYPE*--&gt；查找上述“myService”bean；
 *&lt；bean id=“myServiceFactory”
 *class=“org.springframework.beans.factory.config.ServiceLocatorFactoryBean”&gt；
 *&lt；property name=“serviceLocatorInterface”value=“a.b.c.ServiceFactory”/&gt；
 *&lt/豆&gt；
 *
 *&lt；bean id=“clientBean”class=“a.b.c.MyClientBean”&gt；
 *&lt；property name=“myServiceFactory”ref=“myServiceFactory”/&gt；
 *&lt/豆&gt；
 *
 *&lt/豆类&gt</前>
 *
 *＜p＞然后，伴随的｛@code MyClientBean｝类实现可能
 *看起来像这样：
 *
 *<pre-class=“code”>包a.b.c；
 *
 *公共类MyClientBean{
 *
 *私有ServiceFactory myServiceFactory；
 *
 *//Spring容器提供的实际实现
 *public void setServiceFactory（ServiceFactory myServiceFactory）{
 *this.myServiceFactory=myServiceFactory；
 *    }
 *
 *public void someBusinessMethod（）{
 *//获取一个全新的MyService实例
 *MyService服务=this.myServiceFactory.getService（）；
 *//使用服务对象来实现业务逻辑。。。
 *    }
 *}</pre>
 *
 *＜p＞通过一个通过名称＜/b＞查找bean＜b＞的示例，考虑
 *以下服务定位器接口。再次注意
 *接口不依赖于任何Spring API。
 *
 *<pre-class=“code”>包a.b.c；
 *
 *公共接口ServiceFactory{
 *
 *public MyService getService（String serviceName）；
 *}</pre>
 *
 *＜p＞基于XML的示例配置
 *｛@link org.springframework.beans.factory.BeanFactory｝可能如下所示：
 *
 *<pre-class=“code”>&lt；豆类&gt；
 *
 *&lt；！--原型bean，因为我们有状态（都扩展了MyService）--&gt；
 *&lt；bean id=“specialService”class=“a.b.c.specialService”singleton=“false”/&gt；
 *&lt；bean id=“anotherService”class=“a.b.c.anotherService”singleton=“false”/&gt；
 *
 *&lt；bean id=“myServiceFactory”
 *class=“org.springframework.beans.factory.config.ServiceLocatorFactoryBean”&gt；
 *&lt；property name=“serviceLocatorInterface”value=“a.b.c.ServiceFactory”/&gt；
 *&lt/豆&gt；
 *
 *&lt；bean id=“clientBean”class=“a.b.c.MyClientBean”&gt；
 *&lt；property name=“myServiceFactory”ref=“myServiceFactory”/&gt；
 *&lt/豆&gt；
 *
 *&lt/豆类&gt</前>
 *
 *＜p＞然后，伴随的｛@code MyClientBean｝类实现可能
 *看起来像这样：
 *
 *<pre-class=“code”>包a.b.c；
 *
 *公共类MyClientBean{
 *
 *私有ServiceFactory myServiceFactory；
 *
 *//Spring容器提供的实际实现
 *public void setServiceFactory（ServiceFactory myServiceFactory）{
 *this.myServiceFactory=myServiceFactory；
 *    }
 *
 *public void someBusinessMethod（）{
 *//获取一个全新的MyService实例
 *MyService服务=this.myServiceFactory.getService（“specialService”）；
 *//使用服务对象来实现业务逻辑。。。
 *    }
 *
 *public void otherBusinessMethod（）{
 *//获取一个全新的MyService实例
 *MyService服务=this.myServiceFactory.getService（“另一服务”）；
 *//使用服务对象来实现业务逻辑。。。
 *    }
 *}</pre>
 *
 *＜p＞请参见｛@link ObjectFactoryCreatingFactoryBean｝以获取替代方法。
 *
 *@作者Colin Sampaleanu
 *@作者Juergen Hoeller
 *@自1.1.4
 *@参见#setServiceLocatorInterface
 *@参见#setServiceMappings
 *@参见ObjectFactoryCreatingFactoryBean
 */
public class ServiceLocatorFactoryBean implements FactoryBean<Object>, BeanFactoryAware, InitializingBean {

	@Nullable
	private Class<?> serviceLocatorInterface;

	@Nullable
	private Constructor<Exception> serviceLocatorExceptionConstructor;

	@Nullable
	private Properties serviceMappings;

	@Nullable
	private ListableBeanFactory beanFactory;

	@Nullable
	private Object proxy;


	/**
	 * Set the service locator interface to use, which must have one or more methods with
	 * the signatures {@code MyType xxx()} or {@code MyType xxx(MyIdType id)}
	 * (typically, {@code MyService getService()} or {@code MyService getService(String id)}).
	 * See the {@link ServiceLocatorFactoryBean class-level Javadoc} for
	 * information on the semantics of such methods.
	 */
	/**
	 *设置要使用的服务定位器接口，该接口必须具有一个或多个方法
	 *签名｛@code MyType xxx（）｝或｛@ccode MyType xxx（MyIdType id）｝
	 *（通常为｛@code MyService getService（）｝或｛@ccode MyService getServices（String id）｝）。
	 *请参阅｛@link ServiceLocatorFactoryBean类级Javadoc｝
	 *这些方法的语义信息。
	 */
	public void setServiceLocatorInterface(Class<?> interfaceType) {
		this.serviceLocatorInterface = interfaceType;
	}

	/**
	 * Set the exception class that the service locator should throw if service
	 * lookup failed. The specified exception class must have a constructor
	 * with one of the following parameter types: {@code (String, Throwable)}
	 * or {@code (Throwable)} or {@code (String)}.
	 * <p>If not specified, subclasses of Spring's BeansException will be thrown,
	 * for example NoSuchBeanDefinitionException. As those are unchecked, the
	 * caller does not need to handle them, so it might be acceptable that
	 * Spring exceptions get thrown as long as they are just handled generically.
	 * @see #determineServiceLocatorExceptionConstructor
	 * @see #createServiceLocatorException
	 */
	/**
	 *设置服务定位器在服务时应抛出的异常类
	 *查找失败。指定的异常类必须具有构造函数
	 *具有以下参数类型之一：｛@code（String，Throwable）｝
	 *或｛@code（Throwable）｝或｛@code（String）｝。
	 *＜p＞如果未指定，将抛出Spring的BeansException的子类，
	 *例如NoSuchBeanDefinitionException。由于未选中这些选项
	 *调用者不需要处理它们，所以可以接受
	 *只要只是一般性地处理，就会抛出Spring异常。
	 *@参见#determineServiceLocatorExceptionConstructor
	 *@参见#createServiceLocatorException
	 */
	public void setServiceLocatorExceptionClass(Class<? extends Exception> serviceLocatorExceptionClass) {
		this.serviceLocatorExceptionConstructor =
				determineServiceLocatorExceptionConstructor(serviceLocatorExceptionClass);
	}

	/**
	 * Set mappings between service ids (passed into the service locator)
	 * and bean names (in the bean factory). Service ids that are not defined
	 * here will be treated as bean names as-is.
	 * <p>The empty string as service id key defines the mapping for {@code null} and
	 * empty string, and for factory methods without parameter. If not defined,
	 * a single matching bean will be retrieved from the bean factory.
	 * @param serviceMappings mappings between service ids and bean names,
	 * with service ids as keys as bean names as values
	 */
	/**
	 *设置服务ID之间的映射（传递到服务定位器）
	 *和bean名称（在bean工厂中）。未定义的服务ID
	 *这里将按原样处理bean名称。
	 *＜p＞空字符串作为服务id键定义了｛@code null｝和
	 *空字符串，以及不带参数的工厂方法。如果未定义，
	 *将从bean工厂检索一个匹配的bean。
	 *@param serviceMappings服务ID和bean名称之间的映射，
	 *将服务id作为键，将bean名称作为值
	 */
	public void setServiceMappings(Properties serviceMappings) {
		this.serviceMappings = serviceMappings;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		if (!(beanFactory instanceof ListableBeanFactory lbf)) {
			throw new FatalBeanException(
					"ServiceLocatorFactoryBean needs to run in a BeanFactory that is a ListableBeanFactory");
		}
		this.beanFactory = lbf;
	}

	@Override
	public void afterPropertiesSet() {
		if (this.serviceLocatorInterface == null) {
			throw new IllegalArgumentException("Property 'serviceLocatorInterface' is required");
		}

		// Create service locator proxy.
		// 创建服务定位器代理。
		this.proxy = Proxy.newProxyInstance(
				this.serviceLocatorInterface.getClassLoader(),
				new Class<?>[] {this.serviceLocatorInterface},
				new ServiceLocatorInvocationHandler());
	}


	/**
	 * Determine the constructor to use for the given service locator exception
	 * class. Only called in case of a custom service locator exception.
	 * <p>The default implementation looks for a constructor with one of the
	 * following parameter types: {@code (String, Throwable)}
	 * or {@code (Throwable)} or {@code (String)}.
	 * @param exceptionClass the exception class
	 * @return the constructor to use
	 * @see #setServiceLocatorExceptionClass
	 */
	/**
	 *确定用于给定服务定位器异常的构造函数
	 *类。仅在自定义服务定位器异常的情况下调用。
	 *＜p＞默认实现查找具有
	 *以下参数类型：｛@code（String，Throwable）｝
	 *或｛@code（Throwable）｝或｛@code（String）｝。
	 *@param exceptionClass异常类
	 *@返回要使用的构造函数
	 *@参见#setServiceLocatorExceptionClass
	 */
	@SuppressWarnings("unchecked")
	protected Constructor<Exception> determineServiceLocatorExceptionConstructor(Class<? extends Exception> exceptionClass) {
		try {
			return (Constructor<Exception>) exceptionClass.getConstructor(String.class, Throwable.class);
		}
		catch (NoSuchMethodException ex) {
			try {
				return (Constructor<Exception>) exceptionClass.getConstructor(Throwable.class);
			}
			catch (NoSuchMethodException ex2) {
				try {
					return (Constructor<Exception>) exceptionClass.getConstructor(String.class);
				}
				catch (NoSuchMethodException ex3) {
					throw new IllegalArgumentException(
							"Service locator exception [" + exceptionClass.getName() +
							"] neither has a (String, Throwable) constructor nor a (String) constructor");
				}
			}
		}
	}

	/**
	 * Create a service locator exception for the given cause.
	 * Only called in case of a custom service locator exception.
	 * <p>The default implementation can handle all variations of
	 * message and exception arguments.
	 * @param exceptionConstructor the constructor to use
	 * @param cause the cause of the service lookup failure
	 * @return the service locator exception to throw
	 * @see #setServiceLocatorExceptionClass
	 */
	/**
	 *为给定原因创建服务定位器异常。
	 *仅在自定义服务定位器异常的情况下调用。
	 *＜p＞默认实现可以处理
	 *消息和异常参数。
	 *@param exceptionConstructor要使用的构造函数
	 *@param导致服务查找失败的原因
	 *@return要抛出的服务定位器异常
	 *@参见#setServiceLocatorExceptionClass
	 */
	protected Exception createServiceLocatorException(Constructor<Exception> exceptionConstructor, BeansException cause) {
		Class<?>[] paramTypes = exceptionConstructor.getParameterTypes();
		Object[] args = new Object[paramTypes.length];
		for (int i = 0; i < paramTypes.length; i++) {
			if (String.class == paramTypes[i]) {
				args[i] = cause.getMessage();
			}
			else if (paramTypes[i].isInstance(cause)) {
				args[i] = cause;
			}
		}
		return BeanUtils.instantiateClass(exceptionConstructor, args);
	}


	@Override
	@Nullable
	public Object getObject() {
		return this.proxy;
	}

	@Override
	public Class<?> getObjectType() {
		return this.serviceLocatorInterface;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}


	/**
	 * Invocation handler that delegates service locator calls to the bean factory.
	 */
	/**
	 *将服务定位器调用委托给bean工厂的调用处理程序。
	 */
	private class ServiceLocatorInvocationHandler implements InvocationHandler {

		@Override
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			if (ReflectionUtils.isEqualsMethod(method)) {
				// Only consider equal when proxies are identical.
				// 仅当代理相同时才认为相等。
				return (proxy == args[0]);
			}
			else if (ReflectionUtils.isHashCodeMethod(method)) {
				// Use hashCode of service locator proxy.
				// 使用服务定位器代理的hashCode。
				return System.identityHashCode(proxy);
			}
			else if (ReflectionUtils.isToStringMethod(method)) {
				return "Service locator: " + serviceLocatorInterface;
			}
			else {
				return invokeServiceLocatorMethod(method, args);
			}
		}

		private Object invokeServiceLocatorMethod(Method method, Object[] args) throws Exception {
			Class<?> serviceLocatorMethodReturnType = getServiceLocatorMethodReturnType(method);
			try {
				String beanName = tryGetBeanName(args);
				Assert.state(beanFactory != null, "No BeanFactory available");
				if (StringUtils.hasLength(beanName)) {
					// Service locator for a specific bean name
					// 特定bean名称的服务定位器
					return beanFactory.getBean(beanName, serviceLocatorMethodReturnType);
				}
				else {
					// Service locator for a bean type
					// bean类型的服务定位器
					return beanFactory.getBean(serviceLocatorMethodReturnType);
				}
			}
			catch (BeansException ex) {
				if (serviceLocatorExceptionConstructor != null) {
					throw createServiceLocatorException(serviceLocatorExceptionConstructor, ex);
				}
				throw ex;
			}
		}

		/**
		 * Check whether a service id was passed in.
		 */
		/**
		 *检查是否传入了服务id。
		 */
		private String tryGetBeanName(@Nullable Object[] args) {
			String beanName = "";
			if (args != null && args.length == 1 && args[0] != null) {
				beanName = args[0].toString();
			}
			// Look for explicit serviceId-to-beanName mappings.
			// 查找serviceId到beanName的显式映射。
			if (serviceMappings != null) {
				String mappedName = serviceMappings.getProperty(beanName);
				if (mappedName != null) {
					beanName = mappedName;
				}
			}
			return beanName;
		}

		private Class<?> getServiceLocatorMethodReturnType(Method method) throws NoSuchMethodException {
			Assert.state(serviceLocatorInterface != null, "No service locator interface specified");
			Class<?>[] paramTypes = method.getParameterTypes();
			Method interfaceMethod = serviceLocatorInterface.getMethod(method.getName(), paramTypes);
			Class<?> serviceLocatorReturnType = interfaceMethod.getReturnType();

			// Check whether the method is a valid service locator.
			// 检查该方法是否为有效的服务定位器。
			if (paramTypes.length > 1 || void.class == serviceLocatorReturnType) {
				throw new UnsupportedOperationException(
						"May only call methods with signature '<type> xxx()' or '<type> xxx(<idtype> id)' " +
						"on factory interface, but tried to call: " + interfaceMethod);
			}
			return serviceLocatorReturnType;
		}
	}

}
